package org.adream.account.rest.api;

import java.util.List;

import org.adream.account.entity.BusinessScaleEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.service.BusinessScaleService;
import org.adream.account.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 企业规模
 * @author sl
 *
 */
@RequestMapping("/scale")
@Controller
public class BusinessScaleApiController {
	
	@Autowired
	private BusinessScaleService scaleService;
	
	private final static Logger logger = LoggerFactory.getLogger(BusinessScaleApiController.class);
	
	/**
	 * 新增
	 * @param scale
	 * @return
	 */
	@RequestMapping(value = "", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<String> addScale(@RequestBody BusinessScaleEntity scale) {
		if(scale == null || StringUtils.isEmpty(scale.getBsName())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "新增失败,请求参数有误", null);
		}
		return scaleService.addScale(scale);
	}
	
	/**
	 * 删除
	 * @param bsId
	 * @return
	 */
	@RequestMapping(value = "/{bsId}", produces = "application/json;charset=UTF-8", method = RequestMethod.DELETE)
	@ResponseBody
	public ResultModel<String> deleteScale(@PathVariable("bsId") Integer bsId) {
		if(bsId == null) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "删除失败,请求参数有误", null);
		}
		return scaleService.deleteScale(bsId);
	}
	
	/**
	 * 更新
	 * @param bsId
	 * @param scaleEntity
	 * @return
	 */
	@RequestMapping(value = "/{bsId}", produces = "application/json;charset=UTF-8", method = RequestMethod.PUT)
	@ResponseBody
	public ResultModel<String> updateScale(@PathVariable("bsId") Integer bsId,@RequestBody BusinessScaleEntity scaleEntity) {
		if(bsId == null || scaleEntity == null || StringUtils.isEmpty(scaleEntity.getBsName())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "更新失败,请求参数有误", null);
		}
		return scaleService.updateScale(bsId, scaleEntity);
	}
	
	/**
	 * 根据bsId,删除标志查询
	 * @param bsId
	 * @param dr
	 * @return
	 */
	@RequestMapping(value = "/bsId/{bsId}/dr/{dr}", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<BusinessScaleEntity> queryScaleByNidAndDr(@PathVariable("bsId") Integer bsId,@PathVariable("dr") Integer dr) {
		if(bsId == null || dr == null) {
			return new ResultModel<BusinessScaleEntity>(ResultModel.ERR_PARAM, "查询失败,请求参数有误", null);
		}
		BusinessScaleEntity scale = scaleService.queryScaleByBsIdAndDr(bsId, dr);
		if(scale == null) {
			logger.warn("企业规模数据不存在,bsId:{}", bsId);
			return new ResultModel<BusinessScaleEntity>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<BusinessScaleEntity>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, scale);
	}
	
	/**
	 * 根据删除标志查询
	 * @param dr 删除标志
	 * @return
	 */
	@RequestMapping(value = "/dr/{dr}", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<List<BusinessScaleEntity>> queryScaleByDr(@PathVariable("dr") Integer dr) {
		if(dr == null) {
			return new ResultModel<List<BusinessScaleEntity>>(ResultModel.ERR_PARAM, "查询失败,请求参数有误", null);
		}
		List<BusinessScaleEntity> scales = scaleService.queryScaleByDr(dr);
		if(Utils.isEmptyCollection(scales)) {
			return new ResultModel<List<BusinessScaleEntity>>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<List<BusinessScaleEntity>>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, scales);
	}
}